package my.news;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.aliasi.spell.TfIdfDistance;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;

/**
 * select document which has max score to query doc
 * @author yura
 *
 */
public class TFIDFDocumentSelecteor implements DocumentSetProcessor {
    public List<String> words;
    public double threshold;



    public TFIDFDocumentSelecteor(List<String> words, double threshold) {
        super();
        this.words = words;
        this.threshold = threshold;
    }



    public Set<Content> process(Set<Content> contents) {
        Set<Content> result = new HashSet<Content>();
        TfIdfDistance distance = new TfIdfDistance(IndoEuropeanTokenizerFactory.FACTORY);
        String query = "";
        for(String word:words) {
            query+=" "+word;
        }

        //train
        distance.trainIdf(query);
        for(Content c : contents) {
            distance.trainIdf(c.data);
        }

        //select
        for(Content c : contents) {
            double proximity = distance.proximity(query, c.data);
            if (proximity>threshold) {
                result.add(c);
            }
        }
        return result;
    }

}
